<?php

namespace App\Http\Controllers\Admin\Framework\Manage;

use App\Http\Controllers\Admin\ParentController;
use App\Master\Framework\Extend\DataUtil;
use App\Master\Framework\View\AdminData;
use App\Models\Admin\Framework\AdminGroupModel;
use App\Models\Admin\Framework\AdminUsersModel;
use App\Utils\AppResult;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class GroupController extends ParentController
{

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    public function index(Request $request)
    {
        if ($request->isMethod('post')) {
            return AppResult::response200('success', AdminGroupModel::getGroupPage());
        } else {
            return view('admin.framework.manage.group.index');
        }
    }

    public function insert(Request $request)
    {
        $params = $request->all();
        if (empty($params['name'])) {
            return AppResult::response203('请填写名称！');
        }

        // 初始化空数据
        $form = DataUtil::field($params, ['status']);

        $form['level'] = 1;

        if ($parent_ids = DataUtil::stringToArray($params['parent_id'])) {
            $form['parent_id'] = end($parent_ids) ? end($parent_ids) : 0;
            $form['level']     = count($parent_ids) + 1;
        } else {
            $form['parent_id'] = 0;
        }

        $form['status']     = $params['status'] == 'true' ? 1 : 2;
        $form['created_at'] = time();
        if (!AdminGroupModel::query()->insert($form)) {
            return AppResult::response201('创建失败');
        }
        return AppResult::response200('创建成功');
    }

    public function update(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }
        if (empty($params['name'])) {
            return AppResult::response203('请填写名称！');
        }

        // 初始化空数据
        $form          = DataUtil::field($params, ['status']);
        $form['level'] = 1;

        if ($parent_ids = DataUtil::stringToArray($params['parent_id'])) {
            $form['parent_id'] = end($parent_ids) ?? 0;
            $form['level']     = count($parent_ids) + 1;
        } else {
            $form['parent_id'] = 0;
        }
        $form['status']     = $params['status'] == 'true' ? 1 : 2;
        $form['updated_at'] = time();
        if (!AdminGroupModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    public function status(Request $request)
    {
        $params = $request->all();
        if (empty($params['id']) || !isset($params['status'])) {
            return AppResult::response203('操作超时！');
        }

        if ($params['status'] == 1){
            if (AdminGroupModel::query()->where('parent_id', $params['id'])->whereIn('status', [1, 2])->where('is_delete', 0)->exists()) {
                return AppResult::response201('当前数据存在子集，请先禁用子集！');
            }
        }

        $form['status']  = $params['status'] == 1 ? 2 : 1;
        $form['updated_at'] = time();
        if (!AdminGroupModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }

    public function remove(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            return AppResult::response203('操作超时！');
        }

        if (AdminGroupModel::query()->where('parent_id', $params['id'])->whereIn('status', [1, 2])->where('is_delete', 0)->exists()) {
            return AppResult::response201('当前数据存在子集，请先删除子集！');
        }

        $form['is_delete']  = 1;
        $form['updated_at'] = time();
        if (!AdminGroupModel::query()->where('id', $params['id'])->update($form)) {
            return AppResult::response201('操作失败');
        }
        return AppResult::response200('操作成功');
    }
}